package com.qingke.java.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.java.command.impl.AddCommand;
import com.qingke.java.command.impl.MyInfoCommand;
import com.qingke.java.command.impl.OrderCommand;
import com.qingke.java.command.impl.PayCommand;
import com.qingke.java.command.impl.ShopcartCommand;
import com.qingke.java.pojo.User;

public class UserCommandFactory extends SystemCommandFactory {
	private User user;

	public UserCommandFactory(User user) {
		this.user = user;
	}

	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.ADD, AddCommand.class);
		commandMap.put(CommandCode.ORDER, OrderCommand.class);
		commandMap.put(CommandCode.PAY, PayCommand.class);
		commandMap.put(CommandCode.SHOPCART, ShopcartCommand.class);
		commandMap.put(CommandCode.MYINFO, MyInfoCommand.class);
	}

	@Override
	public Command buildCommand(CommandCode cmdcode) {
		Command command = null;
		Class<? extends Command> cmdClass = commandMap.get(cmdcode);
		if (cmdClass == null) {
			return super.buildCommand(cmdcode);
		}

		try {
			command = cmdClass.getConstructor(User.class).newInstance(user);
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return command;
	}

}
